/* 
 * Create by Loyal Wu.
 * Email: thanhvu@v2t.mobi
 */
var p = 2;
$(document).ready(function(){
$("#menubottom").hide();
$(".term-added").hide();
$(".bottom-sticky-div").hide(); 
});
(function($) {
    $.fn.bottom = function(options) {
        var defaults = {proximity: 0};
        var options = $.extend(defaults, options);
        return this.each(function() {
            var obj = this;
            $(obj).bind("scroll", function() {
                if (obj == window) {
                    scrollHeight = $(document).height();
                }
                else {
                    scrollHeight = $(obj)[0].scrollHeight;
                }
                scrollPosition = $(obj).height() + $(obj).scrollTop();
                if ((scrollHeight - scrollPosition) / scrollHeight <= options.proximity) {
                    $(obj).trigger("bottom");
                }
            });
            return false;
        });
    };
})(jQuery);
function load_data(page) {
    $.ajax({
        type: "GET",
        url: window.location.toString(),
        data: {"page": page},
        dataType: "html"
    }).done(function(data) {
    	console.log(data);
        $('.loading-gif').fadeOut("fast");
        $("#product_list1").append($(data).find("#product_list"+page).html().trim());
    });
}

$(function() {
    $(window).bottom({proximity: 0.1});
    $(window).on('bottom', function() {
        if (p <= max_page) {
            var obj = $(this);
            if (!obj.data('loading')) {
                obj.data('loading', true);
                $('.loading-gif').show();
                $('.loading-gif').css('text-align', 'center');
                $('.loading-gif').html("<a>Đang tải &nbsp; <img src='/images/ajax-loader.gif'/></a>").fadeIn('fast');
                setTimeout(function() {
                    load_data(p);
                    p ++;
                    obj.data('loading', false);

                }, 1000);
            }
        }else{
			$("#menubottom").show();
			if(show_term)
				$(".term-added").show();
			$(".bottom-sticky-div").show();
			$(".bottom-sticky-div").removeClass("bottom-sticky");
			$(".bottom-space").hide();
		}
    });
});
var lastScroll = 0;
$(window).scroll(function() {
if ( p > 2 ) {
    if (lastScroll <= $(window).scrollTop()-5) {
        if( p <= max_page){
		$(".term-added").hide();
		}
	$("#topheader").css("position","relative");
        $(".top-sticky-div").removeClass("top-sticky");
	$(".top-space").height(0);
		
    } else if (lastScroll > $(window).scrollTop()) {
        $(".top-sticky-div").addClass("top-sticky");
	$(".top-space").height($("#menu").height()+$("#topheader").height());
	$("#topheader").css("position","fixed");
	$("#topheader").css("max-width",$("#wapper").width());
		$("#topheader").css("width",$("#wapper").width());
		$("#topheader").css("z-index","10000");
		$("#topheader").css("top","0");
	$("#right_top").css("top","45px");
	$(".top-sticky-div").css("top",$("#topheader").height());
    }
    lastScroll = $(window).scrollTop();
}
});
